;(function($) {
//	var host = "192.168.0.104";
	var host = "123.206.13.87";
	var port = 80;

//	var basepath = "/api";
	var https = false;

	var getURL = function(path) {
		var urlstr = [];
		if (https) {
			urlstr.push('https://');
		} else {
			urlstr.push('http://');
		}
		urlstr.push(host);
		urlstr.push(':');
		urlstr.push(port);
//		urlstr.push(basepath);
		if (path && (path.indexOf("/") > 0)) {
			urlstr.push('/');
		}
		urlstr.push(path);

		return urlstr.join('');
	}

	var getStaticResUrl = function(resource) {
		var resbase = '/upload';
		var resurl = [];
		if (https) {
			resurl.push('https://');
		} else {
			resurl.push('http://');
		}
		resurl.push(host);
		resurl.push(':');
		resurl.push(port);
		resurl.push(resbase);
		resurl.push(resource);
//		return resurl.join('');
		return "";
	}

	var getStaticResUrl_temp = function(resource) {
		var resbase = '/upload';
		var resurl = [];
		if (https) {
			resurl.push('https://');
		} else {
			resurl.push('http://');
		}
		resurl.push(host);
		resurl.push(':');
		resurl.push(port);
		resurl.push(resbase);
		resurl.push(resource);
		return resurl.join('');
	}

	var urls = {
		common : {
			login : getURL('/login'),
			register : getURL('/register'),
			forget : getURL('/api/login/forgetPassword')
		},
		main : {
			getHottestCourses : getURL('/api/course/getHottestCourses'),
			getLatestCourses : getURL('/api/course/getLatestCourses')
		},
		resources:{
			getAllResources : getURL('/api/course/getAllResources'),
			getResourceDetail : getURL('/api/course/getResourceDetail'),
			submitAnswer : getURL('/api/answer/submitAnswer'),
			listDiscusses : getURL('/api/discuss/listDiscusses'),
			submitDiscuss : getURL('/api/discuss/submitDiscuss'),
			getMyCollections : getURL('/api/course/getMyCollections'),
			getStudyDone : getURL('/api/course/getStudyDone'),
			getMyResources : getURL('/api/course/getMyResources'),
			logStudy : getURL('api/studyLog/logStudy')
		},
		user:{
			uploadAvatar: getURL('/api/user/uploadAvatar'),
			changePassword: getURL('/api/user/changePassword'),
			changePhone: getURL('/api/user/changePhone'),
			submitAuthTeacher: getURL('/api/user/submitAuthTeacher'),
			getWallet: getURL('api/user/getWallet'),
			changeBankCard: getURL('api/user/changeBankCard'),
			getBankInfo: getURL('api/user/getBankInfo')
		},
		push:{
			getPushCourses : getURL('api/push/getPushCourses'),
			getPushCoursedetail: getURL('api/push/getPushCoursedetail'),
			listClassesAndFansToPush : getURL('api/push/listClassesAndFansToPush'),
			pushCourses : getURL('api/push/pushCourses'),
			getPushContent : getURL('api/push/getPushContent')
		},
		classes:{
			getClasses : getURL('api/classes/getClasses'),
			joinClass : getURL('api/classes/joinClass'),
			quitClass : getURL('api/classes/quitClass')
		},
		fans:{
			listConcerns : getURL('api/fans/listConcerns'),
			follow : getURL('api/fans/follow'),
			unfollow : getURL('api/fans/unfollow')
		},
		collection:{
			createCollection : getURL('api/collection/createCollection'),
			deleteCollection : getURL('api/collection/deleteCollection')
		},
		order:{
			getPersonalTradeDetails : getURL('api/order/getPersonalTradeDetails'),
			buyCourse : getURL('api/order/buyCourse'),
			recharge : getURL('api/pay/recharge'),
			getPayStatus : getURL('api/pay/getPayStatus'),
			withdrawMoney : getURL('api/pay/withdrawMoney')
		}
	}

	var getmd5text = function(pwd) {
		if (!pwd) {
			return null;
		}
		return hex_md5(pwd).toUpperCase();
	}

	var courseTypeToText = function(tp) {
		switch (tp) {
			case '1':
				return '微课';
			case '2':
				return '直播';
			case '3':
				return '试卷';
			case '4':
				return '讨论';
			default:
				return '未知课程';
		}
	}

	var requestForData = function(data, category, apiname, callback) {
		api.ajax({
				url: urls[category][apiname],
				method: 'post',
				data: data
		}, function(ret, err) {
				api.hideProgress();
				if (ret) {
					if(ret.status=="NOAUTH"){
						closeToLogin();
					}else if (callback){
						callback(ret);
					}
				} else {
					api.toast({
					    msg: '网络错误',
					    duration: 2000,
					    location: 'bottom'
					});
				}
		});
	}

	var setPrefs = function(key,value) {
            api.setPrefs({
            key : key,
            value : value
        });
    }
    var removePrefs = function(key){
    	api.removePrefs({
            key:key
        });
    }
    var getPrefs = function(key,callback){
    	api.getPrefs({
	        key:key
        },function(ret,err){
        	callback(ret);
        });
    }

	var closeToLogin = function(){
		removePrefs('token');
		removePrefs('userInfo');
		api.openWin({
	        name: 'login',
	        url: 'widget://html/common/login.html',
	        bgColor:'widget://image/login/bg.jpg'
        });
        api.closeFrameGroup({
            name: 'homepage'
        });
	}
	
	var usertype = {
		USERTYPE_PLATFORM:1,
		USERTYPE_BRANCH:2,
		USERTYPE_TUTOR:3,
		USERTYPE_TEACHER:4,
		USERTYPE_STUDENT:5
	}
	
	var prefs = {
		setPrefs:setPrefs,
		removePrefs:removePrefs,
		getPrefs:getPrefs
	}

	var zbobj = {
		host:host,
		urls:urls,
		getmd5text : getmd5text,
		courseTypeToText: courseTypeToText,
		requestForData:requestForData,
		getRes:getStaticResUrl,
		getRes_temp:getStaticResUrl_temp,
		prefs:prefs,
		closeToLogin:closeToLogin,
		usertype:usertype
	}

	window.$zb = zbobj;
})(window.jQuery || window.Zepto || window.$);
